package com.coldworks.coldjoke.task;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import com.coldworks.base.BaseThread;
import com.coldworks.base.manager.BaseNetworkManager;
import com.coldworks.base.util.BaseCONST;
import com.coldworks.coldjoke.manager.UserManager;
import com.coldworks.coldjoke.util.NetworkUtil;
import com.coldworks.coldjoke.util.UrlUtil;

public class JudgeTask extends BaseThread {

	private Context context;
	private Handler handler;
	private List<NameValuePair> params;

	public JudgeTask(Context context, Handler handler, String jokeId,
			String opinion, String reportContent) {
		this.context = context;
		this.handler = handler;
		params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("action", "judge"));
		params.add(new BasicNameValuePair("Jokeid", jokeId));
		params.add(new BasicNameValuePair("opinion", opinion));
		if (reportContent != null) {
			params.add(new BasicNameValuePair("content", reportContent));
		}
	}

	@Override
	public void run() {
		Message message = null;
		HttpPost httpPost = new HttpPost(UrlUtil.getJudgeUrl());
		try {
			String sessionId = UserManager.getInstance().getSessionId();
			httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			httpPost.setHeader("Cookie", "webpy_session_id=" + sessionId);
			HttpResponse response = BaseNetworkManager.getInstance()
					.createHttpClient(context).execute(httpPost);
			if (isInterrupted()) {
				log.info("cancelled");
				return;
			}
			if (response.getStatusLine().getStatusCode() == 200) {
				Header contentEncoding = response
						.getFirstHeader("Content-Encoding");
				JSONObject jsonObject = null;
				if (contentEncoding != null
						&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
					jsonObject = NetworkUtil.getInstance().getJsonObjectByGzip(
							response.getEntity().getContent());
				} else {
					jsonObject = NetworkUtil.getInstance().getJSONObject(
							response.getEntity().getContent());
				}
				if (jsonObject == null) {
					log.info("json result == null");
					message = handler.obtainMessage(BaseCONST.OP.ERR_NET);
					return;

				}
				if (jsonObject.getString("success").equals("true")) {
					message = handler.obtainMessage(BaseCONST.OP.SUCC);
					return;
				}
				message = handler.obtainMessage(BaseCONST.OP.FAIL);
				return;
			} else {
				log.warn(String.valueOf(response.getStatusLine()
						.getStatusCode()));
				message = handler.obtainMessage(BaseCONST.OP.ERR_NET);
				return;
			}
		} catch (ClientProtocolException e) {
			log.error("", e);
			message = handler.obtainMessage(BaseCONST.OP.ERR_NET);
		} catch (UnsupportedEncodingException e) {
			log.error("", e);
			message = handler.obtainMessage(BaseCONST.OP.ERR_NET);
		} catch (IOException e) {
			log.error("", e);
			message = handler.obtainMessage(BaseCONST.OP.ERR_NET);
		} catch (ParseException e) {
			log.error("", e);
			message = handler.obtainMessage(BaseCONST.OP.ERR_NET);
		} catch (JSONException e) {
			log.error("", e);
			message = handler.obtainMessage(BaseCONST.OP.ERR_NET);
		} finally {
			handler.sendMessage(message);
		}
	}

}
